package Admin

import (
	"ali-hr/Database"
	"ali-hr/Models"
	"ali-hr/Services/Auth"
	"errors"
)

/**
 * Created by Goland
 * User: wkk alisleepy@hotmail.com
 * Time: 2021/12/18 - 01:14
 * Desc: <登录相关业务处理>
 */

// Login 登录操作，返回token和标识
func Login(name, password string) (string, error) {
	admin := Models.Admin{
		Name:     name,
		Password: password,
	}

	// 根据name和password查询用户信息
	Database.SqlDB.Where(&admin).First(&admin)
	if admin.ID == 0 {
		return "", errors.New("用户名或密码错误")
	}

	// 获取token信息
	token, err := Auth.CreateToken(admin)
	return token, err
}
